package com.getan.boot.module.system.controller;

import com.getan.boot.common.ResponseBuilder;
import com.getan.boot.common.ResponseCodeData;
import com.getan.boot.common.holder.security.SecurityContextHolder;
import com.getan.boot.module.system.domain.bo.LoginBO;
import com.getan.boot.module.system.domain.vo.LoginVO;
import com.getan.boot.module.system.service.biz.LoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 部门管理
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/")
public class LoginController {

    private final LoginService loginService;

    @PostMapping("login")
    public ResponseCodeData<LoginVO> login(@RequestBody @Valid LoginBO.UsernamePasswordBO bo) {
        return ResponseBuilder.ok(loginService.doLogin(bo.getUsername(), bo.getPassword()));
    }

    @PostMapping("checkLogin")
    public ResponseCodeData<LoginVO> checkLogin() {
        return ResponseBuilder.ok(loginService.loginInfo(SecurityContextHolder.getCurrentUserId()));
    }

    @PostMapping("logout")
    public ResponseCodeData<Void> logout() {
        loginService.logout();
        return ResponseBuilder.ok();
    }

}
